//
//  ZCTextView.swift
//  weibo
//
//  Created by 张城 on 16/7/26.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCTextView: UITextView {
    
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }
    
    var placeholder: String?{
        didSet{
            
            placeholderLabel.text = placeholder
            
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        backgroundColor = UIColor.brownColor()
        
        addSubview(placeholderLabel)
        placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 8))
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ZCTextView.valueChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    func valueChange() {
        placeholderLabel.hidden = self.hasText()
    }
    
    private lazy var placeholderLabel: UILabel = {
       let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        return label
    }()
    
    
}
